package com.buddy.learn.algorithm.exam;

/**
 * @author Buddy
 * @date 2021/5/24
 */
public class Solution3 {


    public static void main(String[] args) {


        //链表倒数第k个元素
        LNode root = new LNode(null, null, 1);
        LNode next = null;
        LNode prev = root;
        for (int i = 0; i < 5; i++) {

        }


        LNode res = getLastK(root, 2);

    }

    private static LNode getLastK(LNode root, int k) {
        if (k > root.length) {
            return null;
        }
        LNode firstRoot = root;
        LNode nextRoot = root;
        for (int j = 0; j < k; j++) {
            firstRoot = firstRoot.next;
        }
        for (int i = 0; i < root.length; i++) {

            if (firstRoot.next != null) {
                nextRoot = nextRoot.next;
                firstRoot = firstRoot.next;
            } else {
                return nextRoot;
            }
        }
        return null;
    }

    private static class LNode {
        int length;
        LNode prev;
        LNode next;
        int data;

        public LNode(LNode prev, LNode next, int data) {
            this.prev = prev;
            this.next = next;
            this.data = data;
        }
    }
}
